package com.landian.commons.jpa.criteria;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author caojl
 * @create 2018/03/13
 **/
class AndPredicable implements Predicable {

    private Predicable[] predicableArr;

    private AndPredicable(Predicable... predicableArr) {
        this.predicableArr = predicableArr;
    }

    public static AndPredicable newInstance(Predicable... predicableArr) {
        return new AndPredicable(predicableArr);
    }

    @Override
    public Predicate build(Root<?> root, CriteriaBuilder cb) {
        List<Predicate> list = new ArrayList();
        for (Predicable predicable : predicableArr) {
            list.add(predicable.build(root, cb));
        }
        Predicate[] predicates = list.toArray(new Predicate[list.size()]);
        Predicate or = cb.and(predicates);
        return or;
    }
}